package utils

import (
	"context"

	"github.com/smallnest/rpcx/protocol"
	"github.com/smallnest/rpcx/server"
)

// RPCServer 服务器端实例
type RPCServer struct {
	Name string
	Addr string
	Port string
	s    *server.Server
}

// NewRPCServer 实例化一个服务对象
func NewRPCServer(name, addr, port string) *RPCServer {
	var s = &RPCServer{Name: name, Addr: addr, Port: port}
	s.s = server.NewServer()
	return s
}

// Register 注册一个服务对象
func (s *RPCServer) Register(obj interface{}, name string) {
	s.s.Register(obj, name)
}

// RegisterName 按名称注册服务对象
func (s *RPCServer) RegisterName(name string, obj interface{}, matedata string) {
	s.s.RegisterName(name, obj, matedata)
}

// Serve 启动服务
func (s *RPCServer) Serve() {
	s.s.Serve("tcp", s.Addr+":"+s.Port)
}

// SetAuthFunc 设置验签函数
func (s *RPCServer) SetAuthFunc(fun func(ctx context.Context, req *protocol.Message, token string) error) {
	s.s.AuthFunc = fun
}
